<!-- 用户管理 -->
<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!-- 部门树 -->
      <el-col :lg="4" :xs="24" class="mb-[12px]">
        <DeptTree v-model="queryParams.deptId" @node-click="handleQuery" />
      </el-col>

      <!-- 用户列表 -->
      <el-col :lg="20" :xs="24">
        <div class="search-container">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="手机号" prop="phone">
              <el-input v-model="queryParams.phone" placeholder="手机号" clearable style="width: 200px" @keyup.enter="handleQuery" />
            </el-form-item>

            <el-form-item label="状态" prop="status">
              <el-select v-model="queryParams.status" placeholder="全部" clearable class="!w-[100px]">
                <el-option label="正常" :value="1" />
                <el-option label="冻结" :value="2" />
                <el-option label="禁止抢单" :value="3" />
              </el-select>
            </el-form-item>

            <el-form-item label="创建时间">
              <el-date-picker
                v-model="queryParams.createTime"
                :editable="false"
                class="!w-[240px]"
                type="daterange"
                range-separator="~"
                start-placeholder="开始时间"
                end-placeholder="截止时间"
                value-format="YYYY-MM-DD"
              />
            </el-form-item>

            <el-form-item>
              <el-button type="primary" @click="handleQuery">
                <template #icon><Search /></template>
                搜索
              </el-button>
              <el-button @click="handleResetQuery">
                <template #icon><Refresh /></template>
                重置
              </el-button>
            </el-form-item>
          </el-form>
        </div>

        <el-card shadow="never" class="table-container">
          <template #header>
            <div class="flex-x-between">
              <div>
                <el-button type="success" @click="handleOpenDialog()">
                  <template #icon><Plus /></template>
                  新增
                </el-button>
                <!-- <el-button type="danger" :disabled="removeIds.length === 0" @click="handleDelete()">
                  <template #icon><Delete /></template>
                  删除
                </el-button> -->
              </div>
              <div>
                <el-button class="ml-3" @click="handleOpenImportDialog">
                  <template #icon><Upload /></template>
                  导入
                </el-button>

                <el-button class="ml-3" @click="handleExport">
                  <template #icon><Download /></template>
                  导出
                </el-button>
              </div>
            </div>
          </template>

          <el-table v-loading="loading" :data="pageData" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="50" align="center" />
            <el-table-column key="id" label="编号" align="center" prop="id" />
            <el-table-column label="用户昵称" align="center" prop="name" />

            <el-table-column label="手机号码" align="center" prop="phone" />

            <el-table-column label="状态" align="center" prop="status">
              <template #default="scope">
                <el-tag type="success" v-if="scope.row.status == 1">正常</el-tag>
                <el-tag type="info" v-if="scope.row.status == 2">冻结</el-tag>
                <el-tag type="danger" v-if="scope.row.status == 3">禁止抢单</el-tag>
                <!-- <el-tag :type="scope.row.status == 1 ? 'success' : 'info'">
                  {{ scope.row.status == 1 ? "正常" : scope.row.status == 2 ? "冻结" : "禁止抢单" }}
                </el-tag> -->
              </template>
            </el-table-column>
            <el-table-column label="创建时间" align="center" prop="createTime" />
            <el-table-column label="操作" fixed="right" align="center">
              <template #default="scope">
                <el-button type="primary" size="small" link @click="hancleResetPassword(scope.row)">
                  <template #icon><RefreshLeft /></template>
                  重置密码
                </el-button>
                <el-button type="primary" link size="small" @click="handleOpenDialog(scope.row)">
                  <template #icon><Edit /></template>
                  编辑
                </el-button>
                <el-button type="danger" link size="small" @click="handleDelete(scope.row.id)">
                  <template #icon><Delete /></template>
                  删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>

          <pagination v-if="total > 0" v-model:total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="handleQuery" />
        </el-card>
      </el-col>
    </el-row>

    <!-- 用户表单弹窗 -->
    <el-drawer v-model="dialog.visible" :title="dialog.title" append-to-body @close="handleCloseDialog">
      <el-form ref="userFormRef" :model="formData" :rules="rules" label-width="80px">
        <el-form-item label="手机号码" prop="phone">
          <el-input v-model="formData.phone" placeholder="请输入手机号码" maxlength="11" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <!-- :disabled="dialog.title == '修改用户'" -->

          <el-input v-model="formData.password" placeholder="请输入密码" />
        </el-form-item>

        <el-form-item label="用户昵称" prop="name">
          <el-input v-model="formData.name" placeholder="请输入用户昵称" />
        </el-form-item>

        <el-form-item label="角色" prop="roleIds">
          <el-select v-model="formData.roleIds" multiple placeholder="请选择">
            <el-option v-for="item in roleOptions" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>

        <el-form-item label="状态" prop="status">
          <el-select v-model="formData.status" placeholder="全部" clearable>
            <el-option label="正常" :value="1" />
            <el-option label="冻结" :value="2" />
            <el-option label="禁止抢单" :value="3" />
          </el-select>
          <!-- <el-switch v-model="formData.status" inline-prompt active-text="正常" inactive-text="冻结" :active-value="1" :inactive-value="2" /> -->
        </el-form-item>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
          <el-button @click="handleCloseDialog">取 消</el-button>
        </div>
      </template>
    </el-drawer>

    <!-- 用户导入弹窗 -->
    <UserImport v-model:visible="importDialogVisible" @import-success="handleUserImportSuccess" />
  </div>
</template>

<script setup lang="ts">
defineOptions({
  name: "User",
  inheritAttrs: false,
});

import UserAPI, { UserForm, UserPageQuery, UserPageVO } from "@/api/user";
import DeptAPI from "@/api/dept";
import RoleAPI from "@/api/role";

import DeptTree from "./dept-tree.vue";
import UserImport from "./import.vue";

const queryFormRef = ref(ElForm);
const userFormRef = ref(ElForm);

const loading = ref(false);
const removeIds = ref([]);
const total = ref(0);
const pageData = ref<UserPageVO[]>();
/** 部门下拉选项 */
const deptOptions = ref();
/** 角色下拉选项 */
const roleOptions = ref();
/** 用户查询参数  */
const queryParams = reactive<UserPageQuery>({
  pageNo: 1,
  pageSize: 10,
});

/**  用户弹窗对象  */
const dialog = reactive({
  visible: false,
  title: "",
});

/** 导入弹窗显示状态 */
const importDialogVisible = ref(false);

// 用户表单数据
let formData = reactive<UserForm>({
  status: 1,
});

/** 用户表单校验规则  */
const rules = reactive({
  // password: [{ required: true, message: "请输入密码", trigger: "blur" }],
  phone: [{ required: true, message: "请输入手机号", trigger: "blur" }],
  name: [{ required: true, message: "用户昵称不能为空", trigger: "blur" }],
  roleIds: [{ required: true, message: "用户角色不能为空", trigger: "blur" }],
});

/** 查询 */
function handleQuery() {
  loading.value = true;
  UserAPI.getPage(queryParams)
    .then((data) => {
      console.log("dadada", data.total);

      pageData.value = data.result;
      total.value = data.total * 1;
    })
    .finally(() => {
      loading.value = false;
    });
}

/** 重置查询 */
function handleResetQuery() {
  queryFormRef.value.resetFields();
  queryParams.pageNum = 1;
  queryParams.createTime = undefined;
  handleQuery();
}

/** 行复选框选中记录选中ID集合 */
function handleSelectionChange(selection: any) {
  removeIds.value = selection.map((item: any) => item.id);
}

/** 重置密码 */
function hancleResetPassword(row: { [key: string]: any }) {
  ElMessageBox.prompt("请输入用户「" + row.name + "」的新密码", "重置密码", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
  }).then(
    ({ value }) => {
      if (!value || value.length < 6) {
        // 检查密码是否为空或少于6位
        ElMessage.warning("密码至少需要6位字符，请重新输入");
        return false;
      }
      let params = {
        id: row.id,
        password: value,
      };
      console.log(row.id);
      console.log(value);

      UserAPI.resetPassword(params).then(() => {
        ElMessage.success("密码重置成功，新密码是：" + value);
      });
    },
    () => {
      ElMessage.info("已取消重置密码");
    }
  );
}

/**
 * 打开弹窗
 *
 * @param id 用户ID
 */
async function handleOpenDialog(row: any) {
  dialog.visible = true;
  // 加载角色下拉数据源
  let { result } = await RoleAPI.getOptions();
  roleOptions.value = result;
  // 加载部门下拉数据源
  // deptOptions.value = await DeptAPI.getOptions();

  if (!row) {
    dialog.title = "新增用户";
  } else {
    dialog.title = "修改用户";
    UserAPI.getFormData(row.id).then((data) => {
      console.log("DSADA", data);

      data = {
        id: row.id,
        phone: row.phone,
        password: row.password,
        name: row.name,
        status: row.status,
        roleIds: data.result ? data.result : [],
      };
      console.log("data", data);

      // formData = JSON.parse(JSON.stringify(data));
      Object.assign(formData, { ...data });
    });
  }
}

/** 关闭弹窗 */
function handleCloseDialog() {
  dialog.visible = false;
  userFormRef.value.resetFields();
  userFormRef.value.clearValidate();

  formData.id = undefined;
  formData.status = 1;
}

/** 表单提交 */
const handleSubmit = useThrottleFn(() => {
  userFormRef.value.validate((valid: any) => {
    if (valid) {
      const userId = formData.id;

      loading.value = true;
      if (userId) {
        UserAPI.update(userId, formData)
          .then(() => {
            ElMessage.success("修改用户成功");
            handleCloseDialog();
            handleResetQuery();
          })
          .finally(() => (loading.value = false));
      } else {
        UserAPI.add(formData)
          .then(() => {
            ElMessage.success("新增用户成功");
            handleCloseDialog();
            handleResetQuery();
          })
          .finally(() => (loading.value = false));
      }
    }
  });
}, 3000);

/** 删除用户 */
function handleDelete(id?: number) {
  const userIds = [id || removeIds.value].join(",");
  if (!userIds) {
    ElMessage.warning("请勾选删除项");
    return;
  }

  ElMessageBox.confirm("确认删除用户?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(
    function () {
      loading.value = true;
      UserAPI.deleteByIds(userIds)
        .then(() => {
          ElMessage.success("删除成功");
          handleResetQuery();
        })
        .finally(() => (loading.value = false));
    },
    function () {
      ElMessage.info("已取消删除");
    }
  );
}
/** 打开导入弹窗 */
function handleOpenImportDialog() {
  importDialogVisible.value = true;
}

/** 导入用户成功 */
function handleUserImportSuccess() {
  handleQuery();
}

/** 导出用户 */
function handleExport() {
  UserAPI.export(queryParams).then((response: any) => {
    const fileData = response.data;
    const fileName = decodeURI(response.headers["content-disposition"].split(";")[1].split("=")[1]);
    const fileType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8";

    const blob = new Blob([fileData], { type: fileType });
    const downloadUrl = window.URL.createObjectURL(blob);

    const downloadLink = document.createElement("a");
    downloadLink.href = downloadUrl;
    downloadLink.download = fileName;

    document.body.appendChild(downloadLink);
    downloadLink.click();

    document.body.removeChild(downloadLink);
    window.URL.revokeObjectURL(downloadUrl);
  });
}

onMounted(() => {
  handleQuery();
});
</script>
